package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomerException;
import com.itheima.dao.CategoryMapper;
import com.itheima.domian.Category;
import com.itheima.domian.Dish;
import com.itheima.domian.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public void delete(Long id) {

        //根据分类名称id进行查询菜品数据 如果有数据就是不能删除 抛出异常
        int count = dishService.count(Wrappers.<Dish>lambdaQuery().eq(Dish::getCategoryId, id));
        if (count > 0) {
            //如果大于零 直接抛出异常
            throw new CustomerException("当前分类关联了菜品不能删除");
        }


        //根据分类名称id进行查询套 餐数据 如果有数据就是不能删除 抛出异常
        int count1 = setmealService.count(Wrappers.<Setmeal>lambdaQuery().eq(Setmeal::getCategoryId, id));
        if (count1 > 0) {
            throw new CustomerException("当前分类关联了其他套餐不能删除");
        }
        //最后就是没有相关联的数据 进行删除逻辑
        this.removeById(id);
    }
}
